/********************************************************************************
*                 Copyright (C) 2011 - Las Venturas Playground                  *
*                      http://iv-mp.nl | http://lvp-iv.com                      *
*                                                                               *
*    This program is free software: you can redistribute it and/or modify       *
*    it under the terms of the GNU General Public License as published by       *
*    the Free Software Foundation, either version 3 of the License, or          *
*    (at your option) any later version.                                        *
*                                                                               *
*    This program is distributed in the hope that it will be useful,            *
*    but WITHOUT ANY WARRANTY; without even the implied warranty of             *
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
*    GNU General Public License for more details.                               *
*                                                                               *
*    You should have received a copy of the GNU General Public License          *
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
*                                                                               *
*********************************************************************************
*                                                                               *
*  Las Venturas Playground - clientscript: login.nut                            *
*                                                                               *
*  This file will hold the LVP:IV Login window                                  *
*                                                                               *
********************************************************************************/

//Definitions
local screen = guiGetScreenSize();
local showlogin = false;
local loginError = -1;
local font = GUIFont("tahoma", 12);
local headerfont = GUIFont("pricedown", 32);

local editboxLogin = GUIEditBox();
    editboxLogin.setVisible(false);
    editboxLogin.setPosition(screen[0]/2-160+25, screen[1]/2+50.0, false);
    editboxLogin.setSize(270.0, 20.0, false);
    editboxLogin.setText(getPlayerName(getLocalPlayer()));
    editboxLogin.setProperty("ReadOnly", "true");
    editboxLogin.setProperty("Disabled", "true");

local btnLogin = GUIButton();
    btnLogin.setVisible(false);
    btnLogin.setPosition(screen[0]/2-160+25, screen[1]/2+137.0, false);
    btnLogin.setSize(60.0, 20.0, false);
    btnLogin.setText("Login");

local editboxPassword = GUIEditBox();
    editboxPassword.setVisible(false);
    editboxPassword.setPosition(screen[0]/2-160+25, screen[1]/2+100.0, false);
    editboxPassword.setSize(270.0, 20.0, false);
    editboxPassword.setProperty("MaskText", "true");

local btnClose = GUIButton();
    btnClose.setVisible(false);
    btnClose.setPosition(screen[0]/2-160+235, screen[1]/2+137.0, false);
    btnClose.setSize(60.0, 20.0, false);
    btnClose.setText("Close");

function onFrameRender()
{
    if(showlogin == true)
    {
        // Draw titlebar
        // Get the center of the screen, minus 50% of the size of the rectangle width and height.
        guiDrawRectangle(screen[0]/2-160, screen[1]/2-25, 320.0, 50.0, 0x50505080, false);
        // To center the text, get the center of screen[1], minus 75% of the size of the rectangle width and height.
        headerfont.drawText(screen[0]/2-80,screen[1]/2-25.0, "LVP:IV Login", false);
        
        // Draw main window
        guiDrawRectangle(screen[0]/2-160, screen[1]/2+25.0, 320.0, 150.0, 0x00000080, false);
        font.drawText(screen[0]/2-160+25,screen[1]/2+25.0, "Username", false);
        font.drawText(screen[0]/2-160+25,screen[1]/2+75.0, "Password", false);
    }   
    
    // Wrong username and/or password? Add an aditional rectangle in red with an error message.
    if(loginError == 1)
    {
        guiDrawRectangle(screen[0]/2-160, screen[1]/2+175.0, 320.0, 30.0, 0xFF384545, false);
        font.drawText(screen[0]/2-160+25,screen[1]/2+180.0, "Invalid username and/or password!", false);
    }
    
    if(loginError == 0)
    {
        guiDrawRectangle(screen[0]/2-160, screen[1]/2+175.0, 320.0, 30.0, 0x00FF2145, false);
        font.drawText(screen[0]/2-160+25,screen[1]/2+180.0, "Login successful!", false);
    }
}

addEvent("frameRender", onFrameRender);

function onButtonClick(btnName, bState)
{
    // What to do if the login button is pressed?
    if(btnName == btnLogin.getName())
    {
		// Log the player in
		triggerServerEvent("LogPlayerIn", editboxPassword.getText());
			
        // Clear the password text.
        editboxPassword.setText("");
        
        // Reset the login variable
        loginError = -1;
        
        // Don't show the form
        showlogin = false;
        
        // And make everything invisible.
        guiToggleCursor(false);
        editboxLogin.setVisible(false);
        btnLogin.setVisible(false);
        editboxPassword.setVisible(false);
        btnClose.setVisible(false);      
		toggleChatWindow(true);
    }

    // What to do if the close button is pressed?
    else if(btnName == btnClose.getName())
    {
        // Clear the password text.
        editboxPassword.setText("");
        
        // Reset the login variable
        loginError = -1;
        
        // Don't show the form
        showlogin = false;
        
        // And make everything invisible.
        guiToggleCursor(false);
        editboxLogin.setVisible(false);
        btnLogin.setVisible(false);
        editboxPassword.setVisible(false);
        btnClose.setVisible(false);      
		toggleChatWindow(true);    
    }      
}

addEvent("buttonClick", onButtonClick);

/**
 * This event is called when a user enters a command.
 *
 * @param command The command the user typed.
 */
function onPlayerCommand(command)
{
    // Check if our login function was called.
    if(command == "/login")
    {
        toggleChatWindow(false);
        showlogin = true;
        guiToggleCursor(true);
        editboxLogin.setVisible(true);
        btnLogin.setVisible(true);
        editboxPassword.setVisible(true);
        btnClose.setVisible(true);
        
        // Freeze the player
        triggerServerEvent("freezePlayer", true);  
    }
}

addEvent("playerCommand", onPlayerCommand);